from flask import Blueprint, session, g, redirect, url_for
from .auth import Auth

user_bp = Blueprint('user', __name__)

auth_view = Auth.as_view('auth')


# 用户的登录、登出、注册接口
user_bp.add_url_rule('/auth/<any(login, logout, register):action>',view_func=auth_view,methods=['POST',])

@user_bp.before_app_request
def load_logged_in_user(): 
  user_id = session.get('user_id')
  if user_id:
    g.user = user_id
  else:
    return redirect(url_for('user.auth', action='login'))